package com.example.demo;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

public class JsonToJsBatchWriter {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入文档目录");
        String pathIn = in.nextLine();
        System.out.println("请输入JS文件输出目录");
        String pathOut = in.nextLine();

        Path outputPath = Paths.get(pathOut);
        createDirectoryIfNotExists(outputPath);

        Path dir = Paths.get(pathIn); // 替换为你的目录路径
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
            for (Path entry : stream) {
                // 检查是否是文件并且扩展名是 .txt
                if (Files.isRegularFile(entry) && entry.toString().endsWith(".txt")) {
                    // 打印文件的完整路径
                    Path path = entry.toAbsolutePath();
                    System.out.println(path.toString());
                    start(path.toString(),outputPath.toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//        start(new String());
    }

    private static void createDirectoryIfNotExists(Path path) {
        if (Files.notExists(path)) {
            try {
                Files.createDirectories(path);
                System.out.println("输出目录已创建: " + path.toString());
            } catch (IOException e) {
                System.err.println("创建输出目录时出错: " + e.getMessage());
            }
        } else {
            System.out.println("输出目录已存在: " + path.toString());
        }
    }

    private static void start(String filePath,String pathOut) {
//        String filePath = "/"; // 修改为你的文件路径
        StringBuilder absolutePath  = new StringBuilder(filePath.substring(0, Math.max(filePath.lastIndexOf('/'), filePath.lastIndexOf('\\'))));
//        System.out.println(absolutePath);
        absolutePath.append('\\');
        StringBuilder pathOutTP = new StringBuilder(pathOut);
        if(pathOutTP.charAt(pathOut.length()-1)!='\\'&& pathOutTP.charAt(pathOut.length()-1)=='/'){
            pathOutTP.append('\\');
        }
        CharsetCodeTest charsetCodeTest = new CharsetCodeTest();
        int PressOffset = 18;
        int time = 0;
        try {
            //确定文件编码
            String encoding = charsetCodeTest.getFileCharsetName(filePath);
            // 使用特定编码读取文件内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), encoding));
            StringBuilder content = new StringBuilder();
            String line = reader.readLine();
            while (line != null) {
                content.append(line);
                line = reader.readLine();
            }
            reader.close();

            // 打印内容并解析为 JSON 对象
            StringBuilder jsonString = new StringBuilder(content.toString());
            if(jsonString.charAt(0) == '[') {
                jsonString.deleteCharAt(0);
                jsonString.deleteCharAt(jsonString.length() - 1);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            // 配置 ObjectMapper 忽略未知属性
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//            System.out.println(jsonString);
            SkyStudio skyStudio = objectMapper.readValue(jsonString.toString(), SkyStudio.class);
//            System.out.println(skyStudio.toString());
            //对象转化完成
            //编写js
            time = 60000/skyStudio.getBpm();
//            System.out.println(time);
            charsetCodeTest.writeJsFileFromJson(PressOffset,time,pathOutTP.toString()+'\\'+skyStudio.getName()+".js",skyStudio);
        } catch (IOException e) {
            System.err.println("加载文件失败");
            e.printStackTrace();
        }
    }
}
